package booksys.bob;

import booksys.common.CurrentUser;
import booksys.common.Result;
import booksys.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet({
        "/bob/bobList",
        "/bob/bobDoAdd",
        "/bob/bobDoUpd",
        "/bob/bobDoDel",
})

public class BobAPI extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BobService bobService = new BobService();
        request.setCharacterEncoding("UTF-8");
        String path = request.getServletPath();
        CurrentUser currentUser = (CurrentUser) request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);

        if ("/bob/bobDoAdd".equals(path)) {
            try {
                BobcardDto dto = Utils.getBeanFromRequest(BobcardDto.class, request);

                bobService.addBobcard(dto, currentUser.getUserId());

                Utils.outJson(response, Result.success());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
        else if ("/bob/bobDoUpd".equals(path)) {
            try {
                BobcardDto dto = Utils.getBeanFromRequest(BobcardDto.class, request);

                bobService.updBobcard(dto);

                Utils.outJson(response, Result.success());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
        else if ("/bob/bobDoDel".equals(path)) {
            try {
                BobcardDto dto = Utils.getBeanFromRequest(BobcardDto.class, request);

                bobService.delBobcard(dto);

                Utils.outJson(response, Result.success());
            } catch (Exception e) {
                e.printStackTrace();
                Utils.outJson(response, Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage()));
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //接收客户端的参数,并将参数封装为java对象
        req.setCharacterEncoding("UTF-8");

        QueryDto queryDto = new QueryDto();

        try {
            queryDto.setPageNum(Integer.parseInt(req.getParameter("pageNum")));
        } catch (Exception e) {}

        try {
            queryDto.setPageSize(Integer.parseInt(req.getParameter("pageSize")));
        } catch (Exception e) {}

        queryDto.setBb_id(req.getParameter("bb_id"));
        queryDto.setSt_id(req.getParameter("st_id"));
        queryDto.setBb_start_btime(req.getParameter("bb_start_btime"));
        queryDto.setBb_start_extime(req.getParameter("bb_start_extime"));
        queryDto.setBb_start_retime(req.getParameter("bb_start_retime"));
        queryDto.setBb_end_btime(req.getParameter("bb_end_btime"));
        queryDto.setBb_end_extime(req.getParameter("bb_end_extime"));
        queryDto.setBb_end_retime(req.getParameter("bb_end_retime"));

        //创建业务模型对象（服务对象/Service对象），通过业务对象执行业务方法
        CurrentUser currentUser = (CurrentUser) req.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
        BobService bobService = new BobService();
        Map<String,Object> page = bobService.getBobcardList(currentUser.getUserId(),queryDto);

        //通过视图生成json
        //Utils.outJson(response, bobList);
        Utils.outResult(resp, Result.success(page));
    }
}
